package com.demo.rydemo.d_role_res.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.rydemo.d_resource.entity.DResource;
import com.demo.rydemo.d_resource.service.IDResourceService;
import com.demo.rydemo.d_role_res.service.IDRoleResService;
import com.demo.rydemo.d_role_res.vo.DRoleResVO;
import com.demo.rydemo.result.AjaxResult;
import com.demo.rydemo.result.CoreController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ry
 * @since 2020-08-27
 */
@Api("角色资源相关操作")
@RestController
@RequestMapping("/d_role_res/d-role-res")
public class DRoleResController extends CoreController {

    @Autowired
    IDRoleResService idRoleResService;

    @Autowired
    IDResourceService idResourceService;

    @Autowired
    AjaxResult ajaxResult;

    @ApiOperation("获取角色拥有的资源")
    @GetMapping("/getResByRoleId")
    AjaxResult getResByRoleId(String roleId){
        QueryWrapper<DResource> qw = new QueryWrapper();
        qw.inSql("resource_id","select res_id from d_role_res where role_id='"+roleId+"'");
        return ajaxResult.success(idResourceService.list(qw));
    }

    @ApiOperation("批量新增角色关联资源")
    @PostMapping("/savesByRoleId")
    AjaxResult savesByRoleId(@RequestBody DRoleResVO dRoleResVO){
        Boolean flag = false;
        flag = idRoleResService.savesByRoleId(dRoleResVO);
        if(flag){
            return  ajaxResult.success("批量添加成功");
        }
        return ajaxResult.error("批量添加失败，请联系管理员");
    }
}
